<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%!
	// 查询热门医生,按照关注数量,已关注的不显示
	String hot(Integer uid, Input input) {
		Integer hospital = input.getInt("hospital", -1); // 医院id
		Integer page = input.getInt("page", 1); // 下拉刷新
		Integer itemCount = input.getInt("item_count", 6); // 上拉加载
		if (page <= 0 || itemCount <= 0) {
			return new Message(30041, "参数不合法，请输入正确的页码或限值").toJson();
		}
		org.hibernate.StatelessSession ss = DBTools.db("basedb").getSf().openStatelessSession();
		org.hibernate.SQLQuery sqlQuery = ss.createSQLQuery(String.format("select * from (select u.id as id, u.name as name, u.avatar as avatar, u.title as title,  u.gender as gender, u.json as json, u.type as type, count(o.oid) as attention, h.name as dept_name,h.descr as hospital_name, h.hospital as dept_id from user as u , operate_history as o, hospital as h where u.id=o.oid and o.category=8 and u.status=1 and o.status=1 and u.hospital=999 and o.hospital=999 and h.hospital=u.dept_id group by o.oid  ) as t where t.id not in(select o.oid from operate_history as o where o.uid=%s and o.category=8 and o.status=1) order by t.attention desc", new Object[]{uid}));
		List<Map> experts = sqlQuery.setFirstResult((page - 1) * itemCount).setMaxResults(itemCount).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		for (Map expert:experts) {
			Map jsonMap = JSONUtil.fromJson(expert.remove("json") + "", new TypeReference<Map>(){});
			if (isEmpty(jsonMap))
				jsonMap = new HashMap();
			expert.put("specialty", Tools.getDesc(jsonMap.get("specialty"), "暂无擅长"));
			expert.put("introduce", Tools.getDesc(jsonMap.get("introduce"), "暂无介绍"));
			User user = User.fromMap(expert, User.class);
			expert.put("avatar", getDefaultAvatar(user));
			expert.put("attention", 0);
		}
		List<Map> rankCount = ss.createSQLQuery(String.format("select count(*) as size from (select u.id as id, u.name as name, u.avatar as avatar, u.title as title,  u.gender as gender, u.json as json, u.type as type, count(o.oid) as attention from user as u , operate_history as o,hospital AS h where u.id=o.oid and o.category=8 and u.status=1 and o.status=1 and u.hospital=999 and o.hospital=999 \tAND h.hospital = u.dept_id group by o.oid  ) as t where t.id not in(select o.oid from operate_history as o where o.uid=%s and o.category=8 and o.status=1) order by t.attention desc", new Object[]{uid})).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		Integer size =  Tools.getInt(isEmpty(rankCount)?"0":(rankCount.get(0).get("size") + ""));
		if (ss != null) 
			ss.close();
		Message msg = new Message(0);
		Map resultMap = new HashMap();
		resultMap.put("rank", experts);
		resultMap.put("current_page", page);
		resultMap.put("page_count",  size%itemCount == 0?(size/itemCount):(size/itemCount + 1));
		resultMap.put("item_count",  itemCount);
		if (isEmpty(experts))
			msg.setErrcodeAndErrmsg(30041, "没有更多了");
		msg.put("data", resultMap);
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, hot(token.getUid(), input));
%>
